﻿

namespace SwtorCalculator {
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// Interaction logic for StatAccordion.xaml
    /// </summary>
    public partial class StatAccordion : UserControl {
        private double[] listHeights = { 186, 74, 102, 74, 74, 144 };
        private const double ANI_TIME_MOD = .0018;

        private bool[] isCollapsed = { false, false, false, false, false, false };
        private DoubleAnimation da = new DoubleAnimation();
        private Storyboard sb = new Storyboard();

        private ImageBrush _horizArrow = new ImageBrush( new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/arrowCollapsed.png", UriKind.Absolute ) ) );
        private ImageBrush _vertArrow = new ImageBrush( new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/arrowExpanded.png", UriKind.Absolute ) ) );

        internal AccordionBody BodyOne {
            get { return this.accordionBody1; }
        }

        /// <summary>
        /// StatAccordion no-arg constructor
        /// </summary>
        public StatAccordion() {
            InitializeComponent();
            accordionBody1.accordionHeader.HeaderTitle.Content = "Stats";
            accordionBody2.accordionHeader.HeaderTitle.Content = "Melee";
            accordionBody3.accordionHeader.HeaderTitle.Content = "Force";
            accordionBody4.accordionHeader.HeaderTitle.Content = "Ranged";
            accordionBody5.accordionHeader.HeaderTitle.Content = "Tech";
            accordionBody6.accordionHeader.HeaderTitle.Content = "Defense";

            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );
            accordionBody1.StatList.Items.Add( "" );

            accordionBody2.StatList.Items.Add( "" );
            accordionBody2.StatList.Items.Add( "" );
            accordionBody2.StatList.Items.Add( "" );
            accordionBody2.StatList.Items.Add( "" );
            accordionBody2.StatList.Items.Add( "" );

            accordionBody3.StatList.Items.Add( "" );
            accordionBody3.StatList.Items.Add( "" );
            accordionBody3.StatList.Items.Add( "" );
            accordionBody3.StatList.Items.Add( "" );
            accordionBody3.StatList.Items.Add( "" );
            accordionBody3.StatList.Items.Add( "" );
            accordionBody3.StatList.Items.Add( "" );

            accordionBody4.StatList.Items.Add( "" );
            accordionBody4.StatList.Items.Add( "" );
            accordionBody4.StatList.Items.Add( "" );
            accordionBody4.StatList.Items.Add( "" );
            accordionBody4.StatList.Items.Add( "" );

            accordionBody5.StatList.Items.Add( "" );
            accordionBody5.StatList.Items.Add( "" );
            accordionBody5.StatList.Items.Add( "" );
            accordionBody5.StatList.Items.Add( "" );
            accordionBody5.StatList.Items.Add( "" );

            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
            accordionBody6.StatList.Items.Add( "" );
        }

        internal void UpdateStats() {
            accordionBody1.StatList.Items[0] = string.Format( "Endurance : {0}", Character.Stat.EnduranceTotal );
            accordionBody1.StatList.Items[1] = string.Format( "Strength : {0}", Character.Stat.StrengthTotal );
            accordionBody1.StatList.Items[2] = string.Format( "Aim : {0}", Character.Stat.AimTotal );
            accordionBody1.StatList.Items[3] = string.Format( "Cunning : {0}", Character.Stat.CunningTotal );
            accordionBody1.StatList.Items[4] = string.Format( "Willpower : {0}", Character.Stat.WillpowerTotal );
            accordionBody1.StatList.Items[5] = string.Format( "Presence : {0}", Character.Stat.PresenceTotal );
            accordionBody1.StatList.Items[6] = string.Format( "Accuracy : {0}", Character.Stat.AccuracyStat );
            accordionBody1.StatList.Items[7] = string.Format( "Critical : {0}", Character.Stat.CritGearStat );
            accordionBody1.StatList.Items[8] = string.Format( "Surge : {0}", Character.Stat.SurgeStat );
            accordionBody1.StatList.Items[9] = string.Format( "Alacrity : {0}", Character.Stat.AlacrityStat );
            accordionBody1.StatList.Items[10] = string.Format( "Defense : {0}", Character.Stat.DefenseStat );
            accordionBody1.StatList.Items[11] = string.Format( "Shield : {0}", Character.Stat.ShieldStat );
            accordionBody1.StatList.Items[12] = string.Format( "Absorption : {0}", Character.Stat.AbsorptionStat );

            accordionBody2.StatList.Items[0] = string.Format( "Melee Power Bonus : {0}", Character.Stat.MeleeBonusTotal );
            accordionBody2.StatList.Items[1] = string.Format( "Hit Chance : {0:f}%", Character.Stat.HitTotal * 100 );
            accordionBody2.StatList.Items[2] = string.Format( "Armor Penetration : {0:f}%", Character.Stat.ArPenTotal * 100 );
            accordionBody2.StatList.Items[3] = string.Format( "Critical Chance : {0:f}%", Character.Stat.MeleeCritTotal * 100 );
            accordionBody2.StatList.Items[4] = string.Format( "Critical Damage : {0:f}%", Character.Stat.CritDmgTotal * 100 );

            accordionBody3.StatList.Items[0] = string.Format( "Force Power Bonus : {0}", Character.Stat.ForceBonusTotal );
            accordionBody3.StatList.Items[1] = string.Format( "Hit Chance : {0:f}%", Character.Stat.HitTotal * 100 );
            accordionBody3.StatList.Items[2] = string.Format( "Spell Penetration : {0:f}%", Character.Stat.SpellPenTotal * 100 );
            accordionBody3.StatList.Items[3] = string.Format( "Critical Chance : {0:f}%", Character.Stat.ForceCritTotal * 100 );
            accordionBody3.StatList.Items[4] = string.Format( "Critical Damage : {0:f}%", Character.Stat.CritDmgTotal * 100 );
            accordionBody3.StatList.Items[5] = string.Format( "Cast Speed : {0:f}%", Character.Stat.AlacrityGear * 100 );
            accordionBody3.StatList.Items[6] = string.Format( "Force Regen : {0}", 0 ); // Need to Implement, what effects it other than talents?

            accordionBody4.StatList.Items[0] = string.Format( "Range Power Bonus : {0}", Character.Stat.RangeBonusTotal );
            accordionBody4.StatList.Items[1] = string.Format( "Hit Chance : {0:f}%", Character.Stat.HitTotal * 100 );
            accordionBody4.StatList.Items[2] = string.Format( "Armor Penetration : {0:f}%", Character.Stat.ArPenTotal * 100 );
            accordionBody4.StatList.Items[3] = string.Format( "Critical Chance : {0:f}%", Character.Stat.RangeCritTotal * 100 );
            accordionBody4.StatList.Items[4] = string.Format( "Critical Damage : {0:f}%", Character.Stat.CritDmgTotal * 100 );

            accordionBody5.StatList.Items[0] = string.Format( "Tech Power Bonus : {0}", Character.Stat.TechBonusTotal );
            accordionBody5.StatList.Items[1] = string.Format( "Hit Chance : {0:f}%", Character.Stat.HitTotal * 100 );
            accordionBody5.StatList.Items[2] = string.Format( "Armor Penetration : {0:f}%", Character.Stat.ArPenTotal * 100 );
            accordionBody5.StatList.Items[3] = string.Format( "Critical Chance : {0:f}%", Character.Stat.TechCritTotal * 100 );
            accordionBody5.StatList.Items[4] = string.Format( "Critical Damage : {0:f}%", Character.Stat.CritDmgTotal * 100 );

            accordionBody6.StatList.Items[0] = string.Format( "Health : {0}", Character.Stat.BaseHealthPoint );
            accordionBody6.StatList.Items[1] = string.Format( "Armor : {0}", Character.Stat.ArmorTotal );
            accordionBody6.StatList.Items[2] = string.Format( "Kinetic Reduction : {0:f}%", Character.Stat.KineticReduction * 100 );
            accordionBody6.StatList.Items[3] = string.Format( "Energy Reduction : {0:f}%", Character.Stat.EnergyReduction * 100 );
            accordionBody6.StatList.Items[4] = string.Format( "Internal Reduction : {0:f}%", Character.Stat.InternalReduction * 100 );
            accordionBody6.StatList.Items[5] = string.Format( "Elemental Reduction : {0:f}%", Character.Stat.ElementalReduction * 100 );
            accordionBody6.StatList.Items[6] = string.Format( "Parry : {0:f}%", Character.Stat.ParryGear * 100 );
            accordionBody6.StatList.Items[7] = string.Format( "Deflect : {0:f}%", Character.Stat.DeflectGear * 100 );
            accordionBody6.StatList.Items[8] = string.Format( "Shield Chance : {0:f}%", Character.Stat.ShieldChanceTotal * 100 );
            accordionBody6.StatList.Items[9] = string.Format( "Shield Mitigation : {0}", Character.Stat.AbsorptionGear );
        }

        private void Header_Click( object sender, RoutedEventArgs e ) {
            AccordionBody ab = e.Source as AccordionBody;
            switch( ab.Name ) {
                case "accordionBody1":
                    animateOne();
                    break;
                case "accordionBody2":
                    animateTwo();
                    break;
                case "accordionBody3":
                    animateThree();
                    break;
                case "accordionBody4":
                    animateFour();
                    break;
                case "accordionBody5":
                    animateFive();
                    break;
                case "accordionBody6":
                    animateSix();
                    break;
            }
        }

        private void animateArrow( AccordionBody ab, bool isVert, double time ) {
            DoubleAnimation aniArrow;

            if( isVert ) {
                aniArrow = new DoubleAnimation( 0, 90, new Duration( TimeSpan.FromSeconds( time ) ) );
            }
            else {
                aniArrow = new DoubleAnimation( 90, 0, new Duration( TimeSpan.FromSeconds( time ) ) );
            }

            RotateTransform rt = new RotateTransform();
            ab.accordionHeader.Arrow.RenderTransform = rt;
            ab.accordionHeader.Arrow.RenderTransformOrigin = new Point( 0.5, 0.5 );
            rt.BeginAnimation( RotateTransform.AngleProperty, aniArrow );
        }

        private void animateOne() {
            if( isCollapsed[0] ) {
                da.From = 0;
                da.To = listHeights[0];
                animateArrow( accordionBody1, false, listHeights[0] * ANI_TIME_MOD );
            }
            else {
                da.From = listHeights[0];
                da.To = 0;
                animateArrow( accordionBody1, true, listHeights[0] * ANI_TIME_MOD );
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( listHeights[0] * ANI_TIME_MOD ) );
            sb.Children.Add( da );
            Storyboard.SetTargetName( da, accordionBody1.StatList.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( "(Height)" ) );

            isCollapsed[0] = !isCollapsed[0];

            sb.Begin( accordionBody1.StatList );
        }

        private void animateTwo() {
            if( isCollapsed[1] ) {
                da.From = 0;
                da.To = listHeights[1];
                animateArrow( accordionBody2, false, listHeights[1] * ANI_TIME_MOD );
            }
            else {
                da.From = listHeights[1];
                da.To = 0;
                animateArrow( accordionBody2, true, listHeights[1] * ANI_TIME_MOD );
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( listHeights[1] * ANI_TIME_MOD ) );
            sb.Children.Add( da );
            Storyboard.SetTargetName( da, accordionBody2.StatList.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( "(Height)" ) );

            isCollapsed[1] = !isCollapsed[1];

            sb.Begin( accordionBody2.StatList );
        }

        private void animateThree() {
            if( isCollapsed[2] ) {
                da.From = 0;
                da.To = listHeights[2];
                animateArrow( accordionBody3, false, listHeights[2] * ANI_TIME_MOD );
            }
            else {
                da.From = listHeights[2];
                da.To = 0;
                animateArrow( accordionBody3, true, listHeights[2] * ANI_TIME_MOD );
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( listHeights[2] * ANI_TIME_MOD ) );
            sb.Children.Add( da );
            Storyboard.SetTargetName( da, accordionBody3.StatList.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( "(Height)" ) );

            isCollapsed[2] = !isCollapsed[2];

            sb.Begin( accordionBody3.StatList );
        }

        private void animateFour() {
            if( isCollapsed[3] ) {
                da.From = 0;
                da.To = listHeights[3];
                animateArrow( accordionBody4, false, listHeights[3] * ANI_TIME_MOD );
            }
            else {
                da.From = listHeights[3];
                da.To = 0;
                animateArrow( accordionBody4, true, listHeights[3] * ANI_TIME_MOD );
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( listHeights[3] * ANI_TIME_MOD ) );
            sb.Children.Add( da );
            Storyboard.SetTargetName( da, accordionBody4.StatList.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( "(Height)" ) );

            isCollapsed[3] = !isCollapsed[3];

            sb.Begin( accordionBody4.StatList );
        }

        private void animateFive() {
            if( isCollapsed[4] ) {
                da.From = 0;
                da.To = listHeights[4];
                animateArrow( accordionBody5, false, listHeights[4] * ANI_TIME_MOD );
            }
            else {
                da.From = listHeights[4];
                da.To = 0;
                animateArrow( accordionBody5, true, listHeights[4] * ANI_TIME_MOD );
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( listHeights[4] * ANI_TIME_MOD ) );
            sb.Children.Add( da );
            Storyboard.SetTargetName( da, accordionBody5.StatList.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( "(Height)" ) );

            isCollapsed[4] = !isCollapsed[4];

            sb.Begin( accordionBody5.StatList );
        }

        private void animateSix() {
            if( isCollapsed[5] ) {
                da.From = 0;
                da.To = listHeights[5];
                animateArrow( accordionBody6, false, listHeights[5] * ANI_TIME_MOD );
            }
            else {
                da.From = listHeights[5];
                da.To = 0;
                animateArrow( accordionBody6, true, .2 );
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( listHeights[5] * ANI_TIME_MOD ) );
            sb.Children.Add( da );
            Storyboard.SetTargetName( da, accordionBody6.StatList.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( "(Height)" ) );

            isCollapsed[5] = !isCollapsed[5];

            sb.Begin( accordionBody6.StatList );
        }
    }
}
/*
List 1 : 220
List 2 : 61.6
List 3 : 61.6
List 4 : 61.6
List 5 : 61.6
List 6 : 76
*/